package com.xiaoyu.user.controller;


import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONUtil;
import com.xiaoyu.user.entity.SystemUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequestMapping("redis")
@RestController
@AllArgsConstructor
public class RedisController {

    @Autowired(required = false)
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/string")
    public void string() {
        log.info(redisTemplate.toString());  // 结果： org.springframework.data.redis.core.RedisTemplate@314c8b4a
        redisTemplate.opsForValue().set("string", "redis");
        log.info("string：" + redisTemplate.opsForValue().get("string").toString()); 
    }
    @Data
    @EqualsAndHashCode()
    class SysBean {
        private String name;
        private SystemUser systemUser;
        private List<SystemUser> systemUserList=new ArrayList<>();

    }

    private SysBean getSysBean() {
        SystemUser systemUser = new SystemUser();
        systemUser.setUserName("1");
        List<SystemUser> systemUserList=new ArrayList<>();
        systemUserList.add(systemUser);
        SysBean sysBean = new SysBean();
        sysBean.setName("2");
        sysBean.setSystemUser(systemUser);
        sysBean.setSystemUserList(systemUserList);
        return sysBean;
    }

    private List<SystemUser> getSystemUserList(){
        SystemUser systemUser = new SystemUser();
        systemUser.setUserName("1");
        List<SystemUser> systemUserList=new ArrayList<>();
        return systemUserList;
    }




    @GetMapping("/bean")
    public void bean() {
        redisTemplate.opsForValue().set("sysBean", getSysBean() );
        log.info("sysBean：" + redisTemplate.opsForValue().get("sysBean").toString()); 
    }


    @GetMapping("/jsonStr")
    public void jsonStr() {
        SysBean sysBean = getSysBean();
        String jsonStr = JSONUtil.toJsonStr(sysBean);
        redisTemplate.opsForValue().set("jsonStr", jsonStr);
        log.info("jsonStr：" + redisTemplate.opsForValue().get("jsonStr").toString()); 
    }


    @GetMapping("/list")
    public void list() {
        List<SystemUser> systemUserList = getSystemUserList();
        redisTemplate.opsForValue().set("list", systemUserList);
        log.info("list：" + redisTemplate.opsForValue().get("list").toString()); 
    }


    @GetMapping("/opsForHashBeanField")
    public void opsForHashBeanField() {
        SysBean sysBean = getSysBean();
        redisTemplate.opsForHash().put("opsForHashBean","name", sysBean.getName() );
        log.info("opsForHashBean：" + redisTemplate.opsForHash().get("opsForHashBean","name").toString());
         redisTemplate.opsForHash().put("opsForHashBean","systemUser", sysBean.getSystemUser() );
        log.info("opsForHashBean：" + redisTemplate.opsForHash().get("opsForHashBean","systemUser").toString());
    }



}
